package wangchao.com.rxjavamaster;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private static final String TEST_STR = "hello rx";
    @BindView(R.id.textview)
    TextView tv;
    @BindView(R.id.textview1)
    TextView tv1;
    @BindView(R.id.textview2)
    TextView tv2;
    @BindView(R.id.textview3)
    TextView tv3;
    @BindView(R.id.edx1)
    EditText ed1;
    @BindView(R.id.edx2)
    EditText ed2;
    @BindView(R.id.btn1)
    Button btn;

    private Unbinder bind;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        initFirstRxJava();
        initOperas();
        initSchedule();
        startTimerTask();
        reactivaTask();
    }


    private void startTimerTask() {
        Observable.timer(5, TimeUnit.SECONDS)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Log.d("MainActivity", "call: " + Thread.currentThread().getName());
                    }
                });
    }

    private void initFirstRxJava() {

//        Observable<String> obser = Observable.create(new Observable.OnSubscribe<String>() {
//            @Override
//            public void call(Subscriber<? super String> subscriber) {
//                subscriber.onNext("Hello RxJava!");
//                subscriber.onCompleted();
//            }
//        });

//        Subscriber<String> subscriber = new Subscriber<String>() {
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(String s) {
//                tv.setText(s);
//            }
//        };


//        Observable<String> obser = Observable.just("Hello World");
//
//        Action1<String> action = new Action1<String>() {
//            @Override
//            public void call(String s) {
//                tv.setText(s);
//            }
//        };
//
//        obser.subscribe(action);


        Observable.just(TEST_STR)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        tv.setText(s);
                    }
                });
    }

    /**
     * 操作符相关
     */
    private void initOperas() {
        //map后返回相同的数据类型
//        Observable.just(TEST_STR)
//                .map(new Func1<String, String>() {
//                    @Override
//                    public String call(String s) {
//                        return s + "wangchao";
//                    }
//                })
//                .subscribe(new Action1<String>() {
//                    @Override
//                    public void call(String o) {
//                        tv1.setText(o);
//                    }
//                });


        //map返回不同的数据类型
//         Observable.just(TEST_STR)
//                 .map(new Func1<String, Integer>() {
//                     @Override
//                     public Integer call(String s) {
//                         return s.hashCode();
//                     }
//                 })
//                 .subscribeOn(Schedulers.io())
//                 .observeOn(AndroidSchedulers.mainThread())
//                 .subscribe(new Action1<Integer>() {
//                     @Override
//                     public void call(Integer integer) {
//                        tv1.setText(String.valueOf(integer));
//                     }
//                 });


    }


    //线程切换
    private void initSchedule() {
        Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(Subscriber<? super List<String>> subscriber) {
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < 100; i++) {
                    list.add("wangchao" + i);
                }
                SystemClock.sleep(5000);
                subscriber.onNext(list);
            }
        })
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<String>>() {
                    @Override
                    public void call(List<String> strings) {
                        StringBuilder build = new StringBuilder();
                        for (String s : strings) {
                            build.append(s + "\n");
                        }
                        tv2.setText(build);
                    }
                });
    }

    //响应式编程
    private void reactivaTask() {
        btn.setEnabled(false);

        Observable<CharSequence> name = RxTextView.textChanges(ed1);
        Observable<CharSequence> password = RxTextView.textChanges(ed2);

        Observable.combineLatest(name, password, new Func2<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence name, CharSequence passwrd) {
                return !TextUtils.isEmpty(name) || !TextUtils.isEmpty(passwrd);
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean isLogin) {
                btn.setEnabled(isLogin);
            }
        });

        //防止按钮重复点击
        RxView.clicks(btn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Toast.makeText(MainActivity.this, "im can be cliked!", Toast.LENGTH_LONG).show();
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }

    @OnClick(R.id.btn1)
    void click(View view){
        Toast.makeText(this, "im can be cliked!", Toast.LENGTH_LONG).show();
    }

}
